<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org" xmlns:layOut="http://www.w3.org/1999/xhtml"
      layOut:decorator="layOut/layUI">
<head>
    <title>用户配置菜单</title>
</head>
<body>
<form class="layui-form" id="theForm">
    <div id="theTree" class="demo-tree-more"></div>
    <div>
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="completeFilter">
                <i class="layui-icon layui-icon-ok"></i>提交
            </button>
        </div>
    </div>
</form>

<script type="text/javascript" th:inline="javascript">
    const id = [[${id}]];
    const data = [[${data}]];
</script>
<script type="text/javascript">
    layui.use(['tree','form','jquery'], function(){
        let tree = layui.tree
            ,layer = layui.layer
            ,form = layui.form
            ,$ = layui.jquery

        tree.render({
            elem: '#theTree'
            ,data: data
            ,showCheckbox: true //是否显示复选框
            ,id: 'treeId'
            ,isJump: false //是否允许点击节点时弹出新窗口跳转
        });

        form.on('submit(completeFilter)', function(data) {
            const field = data.field;
            var menuArr = '';
            for(let i in field){
                menuArr += ',' + field[i];
            }
            if (menuArr.length > 1) {menuArr = menuArr.substring(1)}
            console.log(menuArr)

            $.ajax({
                url: "/role/config/"+id+"/"+menuArr,
                type: 'post',
                success: function(result) {
                    console.log(result)
                    if (result) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        }, function() {

                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>